<?php

namespace App\Http\Middleware\Api;
use Closure;
use App\Http\Business\PowerBusiness;
use Illuminate\Auth\Access\AuthorizationException;
class AdminPowerMiddleware
{

    protected $except = [
        'api/v1/admin/manage/routes/routes',
    ];

    public function handle($request, Closure $next)
    {
        $power = $request->path();
        $powerBusiness = new PowerBusiness();
        if ($powerBusiness->checkPower($power) || $this->shouldPassThrough($request)){
            return $next($request);
        }
        throw new AuthorizationException('没有权限操作');
    }

    protected function shouldPassThrough($request)
    {
        foreach ($this->except as $except) {
            if ($except !== '/') {
                $except = trim($except, '/');
            }
            if ($request->is($except)) {
                return true;
            }
        }
        return false;
    }
}
